package model.data;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Iterator;

import model.data.Interface.UserListInterface;
import model.data.Interface.UserListObservable;
import model.data.Interface.UserListObserver;
import model.entity.User;

/**
 * Class UserList : the list of remote users
 * @author Christelle SIAGO HAPPI
 * ChatSystemProject Group G6
 *
 */
public class UserList implements UserListInterface{

    private ArrayList<User> listUser ;
    
    /**
     * creates the userList of remote users
     */
    
    public UserList()
    {
        listUser= new ArrayList<User>() ;
    }
   
    
    /**
     * get the username of a remote user in the list from its adress
     * @param address : the IP address of a user
     */
	@Override
	public String getUserName(InetAddress address) {
		
		boolean bFind = false;
	   
	    String username=null;
		Iterator<User> it =listUser.iterator();
		
        while(it.hasNext() && !bFind) {
            User user= (User)it.next();
            if(user.getIpAdress().equals(address)){
                    bFind= true;
                    username= user.getUsername();
            }			
        }
        return username;
	}

	/**
	 * @return the IP address of a remote user in the list from its login
	 */
	@Override
	public InetAddress getUserAddress(String name) {
		
	    boolean bFind = false;
	    InetAddress inetAddress=null;
		Iterator<User> it =listUser.iterator();
		
        while(it.hasNext() && !bFind) {
            User user= (User)it.next();
            if(user.getUsername().equals(name)){
                    bFind= true;
                    inetAddress= user.getIpAdress();
            }			
        }
        return inetAddress;
	}
/**
 * gets the user by his name
 */
	@Override
	public User getUserByName(String name) {
	    boolean bFind = false;
	    User ret =null;
		Iterator<User> it =listUser.iterator();
		
        while(it.hasNext() && !bFind) {
            User user= (User)it.next();
            if(user.getUsername().equals(name)){
                    bFind= true;
                    ret= user;
            }			
        }
        return ret;
	}

/**
 * get the user by his address
 */

	@Override
	public User getUserByAdress(InetAddress adresse) {
		
		 boolean bFind = false;
		    User ret =null;
			Iterator<User> it =listUser.iterator();
			
	        while(it.hasNext() && !bFind) {
	            User user= (User)it.next();
	            if(user.getIpAdress().equals(adresse)){
	                    bFind= true;
	                    ret= user;
	            }			
	        }
	        return ret;
	}


/**
 * add  user in the list when he gets connected
 * @param user: an entity user
 */
	@Override
	public void addUser(User user) {
		
		listUser.add(user);
		
	}

/**
 * removes a user from the list when he gets disconnected
 */

	@Override
	public void removeUser(User user) {
		
		listUser.remove(user);
		
	}

/**
 * removes all the users in the list
 */

	@Override
	public void removeAll() {
		
		listUser.clear();
	}
	
	/**
    * 
    * @return an iterator from the collection
    */
	    @Override
	    public Iterator iterator() {
	        Iterator<User> it = listUser.iterator();
	        return it;
	    }

/**
 * verify if a user is already in the list
 * @return true if a user is in the userList
 */

	@Override
	public boolean containsName(String name) {
		return (listUser.contains(name)) ;
	}

}
